package com.zch.starter.metrics.prometheus.springboot.starter.config.auto;


import com.zch.starter.metrics.prometheus.springboot.starter.config.properties.HeartBeatConfig;
import com.zch.starter.metrics.prometheus.springboot.starter.config.properties.PrometheusActuatorProperties;
import com.zch.starter.metrics.prometheus.springboot.starter.instance.actuator.LivenessHealthIndicator;
import com.zch.starter.metrics.prometheus.springboot.starter.instance.actuator.ServerResourceMonitor;
import com.zch.starter.metrics.prometheus.springboot.starter.instance.actuator.SystemMonitoringConfig;
import com.zch.starter.metrics.prometheus.springboot.starter.instance.heart.InstanceHeart;
import com.zch.starter.metrics.prometheus.springboot.starter.metrics.ExportMetricsService;
import com.zch.starter.metrics.prometheus.springboot.starter.metrics.RegisterGaugeServer;
import io.micrometer.core.instrument.MeterRegistry;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.actuate.autoconfigure.metrics.MeterRegistryCustomizer;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.scheduling.annotation.EnableScheduling;


/**
 * ConditionalOnProperty条件满足-》MetricsAutoConfiguration交给spring 管理创建-》@PropertySource生效-》MeterRegistry
 * 被spring自动创建-》下面的@Bean 方法都生效
 * ConditionalOnProperty 注解使得prometheus.autoconfigure.enable=true 时，该注解作用的MetricsAutoConfiguration类 才会交给spring管理
 * 也才会执行下面的@Bean 方法
 */
//@Configuration //由于在spring.factories 中org.springframework.boot.autoconfigure
// .EnableAutoConfiguration配置了该类，已经交给spring了，该注解可以去掉了
// 判断prometheus.autoconfigure.enabled的值是否为“true”， matchIfMissing = false：,只在true 时才加载
@EnableScheduling
@Configuration
@ConditionalOnProperty(prefix = "app.heartbeat", name = "enabled", havingValue = "true", matchIfMissing = false)
@EnableConfigurationProperties(value = {HeartBeatConfig.class})
public class ActuatorHealthAutoConfiguration {


    @Bean("livenessHealthIndicator")
    @ConditionalOnMissingBean // 表示只创建一个bean
    LivenessHealthIndicator livenessHealthIndicator() {
        LivenessHealthIndicator livenessHealthIndicator = new LivenessHealthIndicator();
        return livenessHealthIndicator;
    }

    @Bean("systemMonitoringConfig")
    @ConditionalOnMissingBean // 表示只创建一个bean
    SystemMonitoringConfig systemMonitoringConfig(){
        SystemMonitoringConfig config = new SystemMonitoringConfig();
        return config;
    }
    @Bean("serverResourceMonitor")
    @ConditionalOnMissingBean // 表示只创建一个bean
    ServerResourceMonitor serverResourceMonitor(){
        ServerResourceMonitor serverResourceMonitor = new ServerResourceMonitor();
        return serverResourceMonitor;
    }
    @Bean("instanceHeart")
    @ConditionalOnMissingBean // 表示只创建一个bean
    InstanceHeart instanceHeart(){
        InstanceHeart instanceHeart = new InstanceHeart();
        return instanceHeart;
    }

}
